<?php

include_once "facebook.php";

class FacebookConnector
{
	protected $facebook;
	protected $eCommerces;
	protected $user_profile;
	protected $friends;
	protected $user_id;
	
	function __construct () {
		$config = array();
		$config['appId'] = '274399892612353';
		$config['secret'] = 'c9edd9103e0d173bb2c8bdfe17d70d16';
		$config['fileUpload'] = false;

		$this->facebook = new Facebook($config);
		$this->user_id = $this->facebook->getUser();
		
		if($this->user_id) {
			$this->eCommerces = array("pixmania","fnac","cdiscount","mistergooddeal","ebay","ldlc","materiel.net","amazon","priceminister","carrefour","rueducommerce");
			$this->user_profile = $this->facebook->api('/me','GET');
			$this->friends = $this->facebook->api('/me/friends');
		}
		
	}

	
	function fetchAllFriends() {
		return $this->friends['data'];
	}
	
	function getId() {
		return $this->user_id;
	}
	
	function getLoginUrl() {
		return $this->facebook->getLoginUrl();;
	}
	
	function nbFriends() {
		return count($this->friends['data']);
	}
	
	function whoami() {
		return $this->user_profile['name'];
	}
	
	
	function fetchAllLinks($friendId) {
		$arrLinks = array();
		
		if($friendId != "") {
			$Hislikes = "SELECT url FROM url_like WHERE user_id = ".$friendId;

			$resultHislikes = $this->facebook->api( "/fql?q=" . urlencode($Hislikes) );
			
			foreach($resultHislikes['data'] as $like) {
				$link = array();
				foreach($this->eCommerces as $site) {
					if (strpos($like['url'],$site)){
						$tmp1 = preg_replace ( "%http://.*(\.com|\.fr)/%", '' , $like['url']);
						$tmp2 = array();
						preg_match ( "%[^/]*%", $tmp1, $tmp2);
						$tmp3 = preg_replace ( "%-%", ' ' , $tmp2[0]);
						$link['name'] = $tmp3;
						$link['url'] = $like['url'];
						
						$arrLinks[] = $link;
					}
				}
			}
		}
		
		return $arrLinks;
	}
}

?>